package cn.com.header.web.controller.basic;

import cn.com.header.core.util.*;
import cn.com.header.web.model.basic.Subject;
import cn.com.header.web.service.basic.SubjectService;
import net.sf.json.JSONObject;
import org.apache.commons.codec.language.bm.Lang;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import sun.util.locale.LanguageTag;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;


/**
 * 学科控制器
 *
 * @author fengnuoyang
 */
@Controller
@RequestMapping("basic/subject")
public class SubjectController {
    private ExportUtil eu;
    @Resource
    SubjectService subjectSvc;

    /**
     * 课表列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request, HttpServletResponse response) {

        return "basic/subject/list";
    }

    @RequestMapping(value = "context")
    public String context(@RequestParam(defaultValue = "1")Integer pageNo,String subjectName, HttpServletRequest request) {
        Page page=new Page(pageNo);
        if(!LangUtils.isEmpty(subjectName)){
            page.addCond("subjectName","%"+subjectName+"%");
        }
        subjectSvc.query(page);
        request.setAttribute("page",page);
        request.getSession().setAttribute("subjectPage",page);
        return "basic/subject/context";
    }


    @RequestMapping(value="edit")
    public String edit(Long id,HttpServletRequest request) throws Exception {
        Subject subject=new Subject();
        if(id!=null){
            subject =subjectSvc.get(id);
        }
        request.setAttribute("vo",subject);
        return "basic/subject/edit";

    }

    @RequestMapping(value="checkSub")
    @ResponseBody
    public String checkSub(String number,Long id,HttpServletRequest request) throws Exception {
        JSONObject obj = new JSONObject();
       Boolean flag=subjectSvc.isNumberUnique(number,id);
       obj.accumulate("valid", flag);
       return obj.toString();

    }

    @RequestMapping(value = "save")
    public String save(Subject subject, HttpServletRequest request) {
       subjectSvc.saveSubject(subject);
        return "redirect:list.do";
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(Long id) {
        JSONObject obj=new JSONObject();

        try{
            subjectSvc.delete(id);
            obj.accumulate("flag",1);
        }catch(Exception e){
            obj.accumulate("flag",0);
        }
        return  obj.toString();
    }
    @RequestMapping(value = "deleteAll")
    @ResponseBody
    public String deleteAll(Long[] ids) {
        JSONObject obj=new JSONObject();
        try{
            subjectSvc.deleteAll(ids);
            obj.accumulate("flag",1);
        }catch(Exception e){
            obj.accumulate("flag",0);
        }
        return  obj.toString();
    }

}
